package com.softeem.music_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.Album;
import com.softeem.music_sys.service.AlbumService;
import com.softeem.music_sys.mapper.AlbumMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author RD
* @description 针对表【album(专辑信息表)】的数据库操作Service实现
* @createDate 2025-11-07 11:19:16
*/
@Service
public class AlbumServiceImpl extends ServiceImpl<AlbumMapper, Album>
    implements AlbumService{

    @Override
    public Page<Album> getAlbumsBySinger(Integer singerId, Integer page, Integer limit) {
        Page<Album> pageUtil = new Page<>(page, limit);
        QueryWrapper<Album> wrapper = new QueryWrapper<>();
        wrapper.eq("singer_id", singerId);
        wrapper.eq("status", 1);
        wrapper.orderByDesc("release_date");
        return page(pageUtil, wrapper);
    }

    @Override
    public Page<Album> getAlbumsByCompany(String company, Integer page, Integer limit) {
        Page<Album> pageUtil = new Page<>(page, limit);
        QueryWrapper<Album> wrapper = new QueryWrapper<>();
        wrapper.eq("company", company);
        wrapper.eq("status", 1);
        wrapper.orderByDesc("release_date");
        return page(pageUtil, wrapper);
    }

    @Override
    public Page<Album> searchAlbums(String keyword, Integer page, Integer limit) {
        Page<Album> pageUtil = new Page<>(page, limit);
        QueryWrapper<Album> wrapper = new QueryWrapper<>();
        wrapper.like("name", keyword);
        wrapper.eq("status", 1);
        wrapper.orderByDesc("release_date");
        return page(pageUtil, wrapper);
    }

    @Override
    public List<Album> getHotAlbums(Integer limit) {
        QueryWrapper<Album> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderByDesc("release_date");
        wrapper.last("limit " + limit);
        return list(wrapper);
    }
    
    @Override
    public Result addAlbum(Album album) {
        // 设置默认状态
        album.setStatus(1);
        
        boolean result = this.save(album);
        return result ? Result.success().setMessage("添加成功") : Result.fail("添加失败");
    }
    
    @Override
    public Result getAlbumById(Integer id) {
        Album album = this.getById(id);
        if (album == null) {
            return Result.fail("专辑不存在");
        }
        return Result.success().setData(album);
    }
    
    @Override
    public Result updateAlbum(Album album) {
        Album existAlbum = this.getById(album.getId());
        if (existAlbum == null) {
            return Result.fail("专辑不存在");
        }
        boolean result = this.updateById(album);
        return result ? Result.success().setMessage("更新成功") : Result.fail("更新失败");
    }
    
    @Override
    public Result deleteAlbum(Integer id) {
        Album album = this.getById(id);
        if (album == null) {
            return Result.fail("专辑不存在");
        }
        boolean result = this.removeById(id);
        return result ? Result.success().setMessage("删除成功") : Result.fail("删除失败");
    }
    
    @Override
    public Result getAlbumList(Integer page, Integer limit) {
        Page<Album> pageUtil = new Page<>(page, limit);
        Page<Album> resultPage = this.page(pageUtil);
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result searchAlbumsController(String name, Integer page, Integer limit) {
        Page<Album> pageUtil = new Page<>(page, limit);
        QueryWrapper<Album> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        Page<Album> resultPage = this.page(pageUtil, wrapper);
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result getAlbumsBySingerController(Integer singerId, Integer page, Integer limit) {
        Page<Album> pageUtil = new Page<>(page, limit);
        QueryWrapper<Album> wrapper = new QueryWrapper<>();
        wrapper.eq("singer_id", singerId);
        wrapper.eq("status", 1);
        wrapper.orderByDesc("release_date");
        Page<Album> resultPage = this.page(pageUtil, wrapper);
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result getAlbumsByCompanyController(String company, Integer page, Integer limit) {
        Page<Album> pageUtil = new Page<>(page, limit);
        QueryWrapper<Album> wrapper = new QueryWrapper<>();
        wrapper.eq("company", company);
        wrapper.eq("status", 1);
        Page<Album> resultPage = this.page(pageUtil, wrapper);
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result updateStatusController(Integer id, Integer status) {
        Album album = this.getById(id);
        if (album == null) {
            return Result.fail("专辑不存在");
        }
        album.setStatus(status);
        boolean result = this.updateById(album);
        return result ? Result.success().setMessage("状态更新成功") : Result.fail("状态更新失败");
    }
}